<?php

namespace App\Services;
// 引入鉴权类
use App\Services\Factory\UploadingFactory\UploadingFactory;
use Qiniu\Auth;

// 引入上传类
use Qiniu\Storage\UploadManager;

class QniuUploading
{
    //七牛云上传
    public static function uploading($img)
    {
        $accessKey = config('qiniu.accessKey');
        $secretKey = config('qiniu.secretKey');
        $bucket = config('qiniu.bucket');
        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);
        // 生成上传 Token
        $token = $auth->uploadToken($bucket);
        // 要上传文件的本地路径
        $filePath = $img;
        $uploading = UploadingFactory::uploading('baidu');
        $uploadingResult = $uploading->uploading($img);
        if ($uploadingResult != 0)
        {
            return $uploadingResult;
        }
        // 上传到存储后保存的文件名
        $key = date('Y-m-d').time().rand(1111,9999);
        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传。
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath, null, 'application/octet-stream', true, null, 'v2');
        if ($err !== null) {
            return $err;
        } else {
            return $ret;
        }
    }
}
